package org.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.demo.common.Result;
import org.demo.entity.*;
import org.demo.mapper.AppointmentsMapper;
import org.demo.service.IAppointmentsService;
import org.demo.service.ICommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//看房预约
@RestController
@RequestMapping("/appointments")
public class AppointmentsConteoller {
    @Autowired
    private IAppointmentsService appointmentsService;
    @Autowired
    private AppointmentsMapper appointmentsMapper;

    @PostMapping("/add")
    public Result add(@RequestBody Appointments appointments){


        Appointments appointments1 = appointmentsMapper.selectChong(appointments);

        if (appointments1==null){
            appointmentsService.add(appointments);
        }else {
            return Result.error("请勿重复预约");
        }

        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Appointments>> list(@RequestBody (required = false) Appointments appointments){
        List<Appointments> list= appointmentsService.list(appointments);
        return Result.success(list);
    }

    @GetMapping("/Pagelist")
    public Result<PageBean<AppointmentsDto>> Pagelist(@RequestParam Integer pageNum,
                                                      @RequestParam Integer pageSize,
                                                      @ModelAttribute AppointmentsDto appointmentsDto){
        PageBean<AppointmentsDto> pb= appointmentsService.Pagelist(pageNum,pageSize,appointmentsDto);
        return Result.success(pb);
    }


    @PostMapping("/updateAppointmentStatusOk")
    public Result updateAppointmentStatusOk(@RequestBody Appointments appointment){
        Appointments appointments = appointmentsMapper.selectById(appointment.getId());
        if (!appointments.getStatus().equals("0")){
            return Result.error("已完成预约");
        }
        appointmentsService.updateAppointmentStatusOk(appointment);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) { // 使用路径参数 id
        appointmentsService.delete(id);
        return Result.success();
    }

}
